import {EventBus} from "@/components/javaScript/EventBus";

export default {
    provide() {
        return {
            dataSourceMethods: () => this.dataSourceMethods,
        }
    },
    data() {
        return {
            dataSourceMapData: {},
            dataSourceMethods: {},
        }
    },
    methods: {
        /*
        * 自动加载数据源数据
        * */
        requestMethod(formJson) {
            if (formJson) {
                let dataSource = formJson.dataSource;

                if (dataSource && dataSource.length > 0) {
                    for (let i = 0; i < dataSource.length; i++) {
                        let item = dataSource[i];
                        let params = {};
                        if (item.requestParameter && item.requestParameter.length > 0) {
                            item.requestParameter.forEach(rp => {
                                params[rp.key] = rp.value;
                            })
                        }

                        /*
                        * 自动请求（autoRequest）的接口将数据存入 dataSourceMapData之中，key值为数据源名称
                        * */
                        if (item.autoRequest) {
                            let structure = item.structure.split(".");
                            this.$axios[item.requestType]('/api' + item.url, {params: params}).then(res => {
                                let data = this.deepCopy(res);
                                // 根据用户所写结构获取数据
                                for (let i = 0; i < structure.length; i++) {
                                    data = data[structure[i]]
                                }
                                this.dataSourceMapData[item.name] = data;

                                EventBus.$emit(item.name,data)
                            })
                        }

                        /*
                        * 将所有接口放入$dataSourceMethods，以便再次调用
                        * params 为静态参数
                        * p 为动态参数
                        * */
                        this.dataSourceMethods[item.name] = (p) => {
                            return new Promise((resolve) => {
                                let structure = item.structure.split(".");
                                let param = {params: {...params, ...p}};
                                if (item.requestType === "post") {
                                    param = {...params, ...p};
                                }
                                this.$axios[item.requestType]('/api' + item.url, param).then(res => {
                                    let data = this.deepCopy(res);
                                    // 根据用户所写结构获取数据
                                    for (let i = 0; i < structure.length; i++) {
                                        data = data[structure[i]]
                                    }
                                    this.dataSourceMapData[item.name] = data;

                                    EventBus.$emit(item.name,data);

                                    resolve(res);
                                })
                            })
                        }
                    }
                }
            }
        },
        /*
        * 将在线编辑的脚本组册到当前this
        * */
        injectScript(formJson) {
            if (formJson) {
                let {pageScript} = formJson;

                if (pageScript.data.length > 0) {
                    let obj = JSON.parse(pageScript.data)
                    for (const key in obj) {
                        this.$set(this, key, obj[key])
                    }
                }

                if (pageScript && pageScript.methods && pageScript.methods.length > 0) {
                    pageScript.methods.forEach(item => {
                        let obj = {};
                        obj[item.funName] = (new Function("arguments", item.funBody)).bind(this)
                        Object.assign(this, obj);
                    })
                }
            }
        }
    },
    mounted() {
        // this.injectScript(this.formJsonObj);
        if (this.pageScript && JSON.stringify(this.pageScript) !== "{}") {
            let {mounted} = this.pageScript;
            if (mounted.length !== 0) {
                let m = new Function(mounted).bind(this)
                this.$nextTick(() => {
                    m();
                })
            }
        }
    },
    created() {
        if (this.pageScript && JSON.stringify(this.pageScript) !== "{}") {
            let {created} = this.pageScript;
            if (created.length !== 0) {
                let c = new Function(created).bind(this)
                c();
            }
        }
    },
    beforeCreate() {
        this.$nextTick(() => {
            this.requestMethod(this.formJsonObj)
        })
    }
}
